package uk.ac.salfordcc.sccmobile;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
//import android.widget.Toast;

public class MainActivity extends Activity {
    private PackageInfo info;

    @Override
    protected void onResume(){
        super.onResume();
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        // If there are no shared preferences in place, then they must be set up in order to continue.
        // Else the PIN session needs to be checked ensuring that the application is suitably unlocked.
        if(sp.getAll().size() == 0){ this.checkPrefDetails(); }
        else{ SharedUtilities.checkSessionPIN(this, MainActivity.this); }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try{
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            ActionBar bar = getActionBar();
            bar.setTitle(bar.getTitle()+ "- " + info.versionName);
        }
        catch(Exception ex){}





        /*** Homescreen Image View ***/
        GridView gridview = (GridView) findViewById(R.id.appSections);
        gridview.setAdapter(new ImageAdapter(this));
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //Toast.makeText(MainActivity.this, mThumbText[i], Toast.LENGTH_SHORT).show();
                Intent intent;
                switch(i){
                    case 0:	intent = new Intent(MainActivity.this, StaffNewsActivity.class); break;
                    case 1: intent = new Intent(MainActivity.this, SMSActivity.class); break;
                    case 2: intent = new Intent(MainActivity.this, Main_Large.class); break;
                    case 3: intent = new Intent(MainActivity.this, SettingsActivity.class); break;
                    case 4: intent = new Intent(MainActivity.this, ITServicesActivity.class); break;
                    case 5: intent = new Intent(MainActivity.this, FeedbackActivity.class); break;
                    case 6: intent = new Intent(MainActivity.this, mapTestActivity.class); break;
                    default: return;
                }
                startActivity(intent);
            }
        });
    }

    public void checkPrefDetails(){
        Log.e("SETTINGS", ".. need to record username");
        Intent intent = new Intent(this, StoreCredentials.class);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        Intent intent;
    	switch(item.getItemId()){
	    	case R.id.action_exitapp:
	    		SharedUtilities.endApplication(this, MainActivity.this);
	    		return true;
            case R.id.action_settings:
                intent = new Intent(MainActivity.this, SettingsActivity.class);
                startActivity(intent);
                return true;
            case R.id.action_about:
                intent = new Intent(MainActivity.this, AboutThis.class);
                startActivity(intent);
    		default:
    			return super.onOptionsItemSelected(item);
    	}

    } 

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBackPressed(){
        SharedUtilities.clearPinExpiration(MainActivity.this);
        finish();   // End the activity.
    }

}
